package com.py.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.py.common.exception.CustomException;
import com.py.model.admin.dtos.ChannelDto;
import com.py.model.common.dtos.PageResponseResult;
import com.py.model.common.dtos.ResponseResult;
import com.py.model.common.enums.AppHttpCodeEnum;
import com.py.model.wemedia.pojos.WmChannel;
import com.py.wemedia.mapper.WmChannelMapper;
import com.py.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    @Autowired
    private WmChannelService channelService;

    /**
     * 新增
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {

        //参数校验 频道名称不能重复 需要查询是否存在相同名称
        if (channelService.getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName())) != null) {
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
        channelService.save(wmChannel);

        //WmChannel channel = channelService.getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAll(ChannelDto dto) {

        //校验前端参数 page size
        dto.checkParam();
        //构造查询
        IPage page = new Page(dto.getPage(), dto.getSize());

        if (!dto.getName().isEmpty()) {
            //构造查询
            LambdaQueryWrapper<WmChannel> lqw = new LambdaQueryWrapper<>();
            //模糊查询
            lqw.like(!StringUtils.isBlank(dto.getName()),WmChannel::getName,dto.getName());
            lqw.orderByDesc(WmChannel::getStatus);
            lqw.orderByDesc(WmChannel::getCreatedTime);
            //分页查询
            page(page, lqw);

            PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
            result.setData(page.getRecords());

            return result;
        }


        page(page);

        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }


    /**
     * 修改
     *
     * @param wmChannel
     */
    @Override
    public ResponseResult update(WmChannel wmChannel) {


        //通过id修改数据库
        if (wmChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        channelService.updateById(wmChannel);

        return ResponseResult.okResult(wmChannel);
    }

    /**
     * 删除频道
     * @param id
     */
    @Override
    public ResponseResult delete(Integer id) {

        //通过id修改数据库
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmChannel wmChannel = channelService.getById(id);

        if (wmChannel.getStatus()==true){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }

        channelService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}